<template>
  <div>
    <el-dialog
      title="拖动验证"
      :visible.sync="dialogVisible"
      :width="`${originalW+50}px`"
      :close-on-click-modal="false"
      :before-close="close"
    >
      <div
        v-loading="loading"
        style="position: relative"
        @touchmove="mousemove"
        @mousemove="mousemove"
        @mousedown="mousedown"
        @touchstart="mousedown"
      >
        <el-image
          style="width: 100%;pointer-events: none;position: absolute"
          :style="`width: ${originalW}px;height: ${originalH}px`"
          :src="'data:image/png;base64,'+bigImage"
        />
        <div>
          <el-image
            class="small-image"
            :style="`top:${top}px;left:${left}px;pointer-events: none;cursor: pointer;`"
            :src="'data:image/png;base64,'+smallImage"
          />
        </div>
        <div style="padding-top: 218px;padding-left: 63px;user-select:none">
          请拖动左侧图片到缺口处进行验证
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getImageDragVerifyCode } from '@/api/user'

export default {
  name: 'ImageVerifyCode',
  data() {
    return {
      dialogVisible: false,
      smallImage: '',
      bigImage: '',
      top: '0',
      left: '0',
      offset: '0',
      originalH: 0,
      originalW: 343,
      loading: false,
      clickIng: false
    }
  },
  created() {
    window.document.addEventListener('mouseup', this.mouseupListener)
    window.document.addEventListener('touchend', this.mouseupListener)
  },
  beforeDestroy() {
    window.document.removeEventListener('mouseup', this.mouseupListener)
    window.document.removeEventListener('touchend', this.mouseupListener)
  },
  methods: {
    showLoading() {
      this.loading = true
    },
    closeLoading() {
      this.loading = false
    },
    mouseupListener(e) {
      console.log('鼠标抬起')
      this.clickIng = false
      // 没有移动图片
      if (this.top === (this.originalH + 5) && this.left === '0') {
        return
      }
      if (this.top === '0' && this.left === '0') {
        return
      }
      this.$emit('checkOffset', {
        w: this.left,
        h: this.top
      }, this.uid)
    },
    open() {
      this.dialogVisible = true
      getImageDragVerifyCode().then(res => {
        const resObj = res.obj
        this.bigImage = resObj.bigImage
        this.smallImage = resObj.smallImage
        this.smallImage = resObj.smallImage
        this.originalH = resObj.originalH
        this.originalW = resObj.originalW
        this.uid = resObj.uid
        // this.top = (resObj.h/resObj.originalH)*100
        this.top = this.originalH + 5
      })
    },
    reset() {
      this.top = this.originalH + 5
      this.left = '0'
    },
    close() {
      this.$emit('close')
      this.dialogVisible = false
    },
    mousemove(e) {
      console.log('mousemove')
      // e.clientX
      if (!this.clickIng) {
        return
      }
      console.log(e)
      // y轴高度
      let x = e.layerX
      let y = e.layerY
      if (x === undefined && y === undefined) {
        const maxH = document.documentElement.clientHeight
        const maxW = document.documentElement.clientWidth
        x = e.touches[0].clientX - (maxH / 34)
        y = e.touches[0].clientY - (maxW / 2)
      }
      this.top = y - 27
      this.left = x - 22
      console.log('left=', this.left + ',top=' + this.top)
    },
    mousedown(e) {
      console.log('鼠标按下')
      this.clickIng = true
    },
    clickRefreshCode() {

    }
  }

}
</script>

<style scoped lang="scss">
.small-image{
  width: 15%;position: absolute;left: 0px;
  cursor:pointer;
}
.speed-progress-box{
  border: 2px solid #f4f4f3;
  background-color: #f4f4f3;
  height: 40px;
  .speed-progress{
    font-size: 20px;
    line-height: 40px;
    text-align: center;
    width: 40px;
    height: 36px;
    background-color: #fff;
  }
}
.refresh-code{
  width: 25px;
  height: 25px;
  vertical-align: middle;
  margin-left: 20px;
  cursor:pointer;
}
</style>
